///////////////////////////////////////////////////////////////////////////
// Copyright 2009 Arabic Game Developer Network, All rights reserved
// www.agd-online.com - info@agdn-online.com
//
// File: sdlrenderer.cpp
// Description:
//
///////////////////////////////////////////////////////////////////////////

// This file is part of kvalleyX.
// 
// kvalleyX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// kvalleyX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with kvalleyX.  If not, see <http://www.gnu.org/licenses/>.

/////////////////// #includes /////////////////////////////////////////////
#include "sdlrenderer.h"
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>

SDLRenderer::SDLRenderer() :
m_Screen(NULL)
{
}

bool SDLRenderer::init(uint32_t screenWidth, uint32_t screenHeight, 
					   bool fullScreen)
{
	m_ScreenWidth = screenWidth;
	m_ScreenHeight = screenHeight;
	m_FullScreen = fullScreen;
	// initialize SDL
	if(SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		std::cerr << "ERROR: Could not initalize SDL: " << SDL_GetError()<<"\n";
		return false;
	}
	// initialize SDL_image
	///FIXME: version print out is not working
	SDL_version compile_version;
	const SDL_version *link_version = IMG_Linked_Version();
	if(link_version != NULL)
	{
		SDL_IMAGE_VERSION(&compile_version);
		std::cerr << "compiled with SDL_image version: " 
		<< compile_version.major << "." << compile_version.minor 
		<< "." << compile_version.patch << std::endl;
		std::cerr << "running with SDL_image version: " << link_version->major
		<< "." << link_version->minor << "." << link_version->patch <<
		std::endl;
	}
	
	int flags = IMG_INIT_PNG;
	if(IMG_Init(flags) & flags != flags)
	{
		std::cerr << "ERROR: Could not initialze SDL_image: " << IMG_GetError()
		<< std::endl;
		return false;
	}
	
	return true;
}

bool SDLRenderer::showWindow()
{
	std::cerr << "hello SDLRenderer::showWindow\n";
	m_FullScreen = false; ///FIXME: check for how to recover from fullscreen
	// attempt to set up the screen.
	Uint32 flags = SDL_HWSURFACE | SDL_DOUBLEBUF |
	(!m_FullScreen) ? 0 : SDL_FULLSCREEN;
	m_Screen = SDL_SetVideoMode(m_ScreenWidth, m_ScreenHeight, 0, flags);
	if(m_Screen == NULL)
	{
		std::cerr << "ERROR: Could not set video mode: " << SDL_GetError()
		<< "\n";
		return false;
	}
	
	SDL_WM_SetCaption("Valley2", "Valley2");///TODO: Add official caption here
	SDL_ShowCursor(SDL_DISABLE);
	
	return true;
}

///load an optimized img : same as main screen; to a fast blit
SDL_Surface* SDLRenderer::loadImageEx(const char* filePath)
{
	SDL_Surface* oldImage = IMG_Load(filePath);
	SDL_Surface* newImage = NULL ;
	
	if(oldImage == NULL)
	{
		std::cerr << "ERROR: Could not load image " << filePath
		<< " :" << IMG_GetError() << std::endl;
		return false;
	}
	else 
	{
		newImage = SDL_DisplayFormat(oldImage);
		SDL_FreeSurface(oldImage);
	}
	return newImage;
}

bool SDLRenderer::drawImage(const char *filePath, int x, int y)
{
	if(!m_Screen || !filePath)
		return false;
	SDL_Surface *image = loadImageEx(filePath);
		
	if(SDL_BlitSurface(image, NULL, m_Screen, NULL) < 0)
	{
		std::cerr << "ERROR: Could not display image: " << SDL_GetError()
		<< std::endl;
		return false;
	}
	SDL_UpdateRect(m_Screen, x, y, image->w, image->h);
	SDL_FreeSurface(image);
	return true;
}

void SDLRenderer::renderFrame()
{
	//sdl switch buffers.
	
}

SDLRenderer::~SDLRenderer()
{
	IMG_Quit();
	SDL_Quit();// consider move to core2
}
